<template>
  <div style="height:100%">
    <div class="question" v-if="!show">
      <div class="top">
        <span class="topTitle">面试宝典</span>
        <CityChange :questionFilters="questionFilters" v-model="cityName">
        </CityChange>
      </div>
      <van-tabs v-model="active" @click="tabClick">
        <van-tab
          v-for="(item, index) in questionFilters.cityPositions[cityName]"
          :key="item.id"
          :name="index"
          :title="item.name"
        >
        </van-tab>
      </van-tabs>
      <div class="main">
        <div class="left">
          <span class="span">
            <div class="bgc">
              <i class="iconfont iconicon_mine_cuoti myIcon"></i>
              <p class="text">常错题库</p>
            </div>
          </span>
          <span class="span" @click="$router.push('/')">
            <div class="bgc two">
              <i class="iconfont iconicon_mine_qiyeshoucang myIcon"></i>
              <p class="text">企业题库</p>
            </div></span
          >
        </div>
        <div class="circle" @click="simulate">
          <van-circle
            v-model="allQuestion"
            :color="gradientColor"
            :rate="(myAnswer / currentRate) * 100"
            :speed="50"
            :stroke-width="60"
          >
            <template>
              <div class="circleTxt">
                <p>顺序刷题</p>
                <span class="data">{{ myAnswer }}/{{ currentRate }}</span>
              </div>
            </template>
          </van-circle>
        </div>
        <div class="right">
          <span class="span">
            <div class="bgc three">
              <i class="iconfont iconicon_mine_tikushoucang myIcon"></i>
              <p class="text">收藏题库</p>
            </div>
          </span>
          <span class="span">
            <div class="bgc four">
              <i class="iconfont iconicon_mine_mianjing myIcon"></i>
              <p class="text">已答题库</p>
            </div></span
          >
        </div>
      </div>
      <div class="num">
        <span class="title">累计收录</span>
        <span class="agg">{{ currentRate }}</span>
      </div>
      <div class="btn">
        <van-button @click="simulate">模拟面试</van-button>
      </div>
    </div>
  </div>
</template>
<script>
import { interviewFilters, interviewQuestions } from '@/api/question.js'
export default {
  name: 'Questions',
  data () {
    return {
      // tab栏索引
      show: false,
      active: 0,
      tabList: [],
      currentRate:
        (localStorage.getItem('allId') &&
          JSON.parse(localStorage.getItem('allId')).length) ||
        0,
      gradientColor: {
        '30%': '#f4a0b5',
        '70%': '#E40137'
      },
      questionFilters: {},
      cityName: '北京',
      // workType: '',
      allQuestion:
        (localStorage.getItem('allId') &&
          JSON.parse(localStorage.getItem('allId')).length) ||
        0,
      userAnswer:
        (localStorage.getItem('localQuestion') &&
          JSON.parse(localStorage.getItem('localQuestion')).length) ||
        0,
      myAnswer: this.$route.query.myAnswer || 0
    }
  },
  computed: {
    workType () {
      return this.questionFilters.cityPositions[this.cityName][this.active].id
    }
  },
  created () {
    this.show = true
    this.$toast.loading({ message: '加载中' })
    this.getFilters()
  },
  activated () {
    this.myAnswer = this.$route.query.myAnswer || 0
  },
  watch: {
    cityName: {
      handler (newVal) {
        this.active = 0
        this.userAnswer = 0
        this.myAnswer = 0
        localStorage.setItem('localQuestion', JSON.stringify([]))
        localStorage.setItem('questionId', '')
        localStorage.setItem('lenLen', '0')
        this.getquestion()
      }
    }
  },
  methods: {
    // 模拟面试
    simulate () {
      this.$router.push({
        name: 'PlayQuestions',
        params: {
          type: +this.workType,
          city: this.cityName
        }
      })
    },
    // 获取筛选条件
    async getFilters () {
      const res = await interviewFilters()
      this.questionFilters = res.data
      this.$store.commit('question/setQuestionFilters', res.data)
      // 默认选中第一个tab栏标签
      this.active = 0
      this.$nextTick(() => {
        this.$toast.clear()
        this.show = false
      })
      this.getquestion()
    },
    // 获取面试数据,渲染进度条
    async getquestion () {
      const res = await interviewQuestions({
        type: +this.workType,
        city: this.cityName
      })
      localStorage.setItem('allId', JSON.stringify(res.data))
      this.currentRate = res.data.length
    },
    // tabs顶部标签栏点击
    tabClick (name) {
      this.userAnswer = 0
      localStorage.setItem('questionId', '')
      localStorage.setItem('lenLen', '0')
      this.myAnswer = 0

      localStorage.setItem('localQuestion', JSON.stringify([]))
      this.getquestion()
    }
  }
}
</script>
<style scoped lang="less">
::v-deep .van-tab__text {
  padding: 0 20px;
  background: #f7f4f5;
  border-radius: 32px;
  font-family: PingFangSC-Regular;
  font-size: 24px;
  color: #545671;
  letter-spacing: 0.28px;
  font-weight: 400;
}
::v-deep .van-tabs__wrap--scrollable .van-tab {
  padding: 0 5px;
}
::v-deep .van-index-bar__index {
  padding-bottom: 30px;
}
.question {
  height: 100%;
  width: 100%;
  background: #f7f4f5;
  overflow-y: hidden;
  // padding-bottom: 100px;
  .top {
    height: 100px;
    text-align: center;
    padding-top: 30px;
    background-color: #fff;
    .topTitle {
      font-size: 36px;
      color: #222222;
      letter-spacing: 0.41px;
      font-weight: 600;
    }
  }
  .main {
    width: 100%;
    margin: 30px auto;
    background: #fff;
    display: flex;
    align-items: center;
    position: relative;
    .left {
      flex: 1;
      height: 100%;
    }
    .right {
      flex: 1;
      height: 100%;
    }
    .span {
      display: block;
      width: 100%;
      height: 200px;
      position: relative;
      .bgc {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -70%);
        width: 88px;
        height: 88px;
        background-image: linear-gradient(180deg, #ff8080 0%, #ff4949 100%);
        text-align: center;
        border-radius: 50%;
        .myIcon {
          font-size: 48px;
          line-height: 88px;
        }
        .text {
          margin-top: 10px;
          font-size: 24px;
          color: #545671;
          word-break: keep-all;
          white-space: nowrap;
          text-indent: -5px;
        }
      }
      .two {
        background-image: linear-gradient(180deg, #00ddec 0%, #00b8d4 100%);
      }
      .three {
        background-image: linear-gradient(180deg, #ffda05 0%, #ffb302 100%);
      }
      .four {
        background-image: linear-gradient(180deg, #3ee5b1 0%, #1dc779 100%);
      }
    }
    .circle {
      width: 238px;
      height: 238px;
      .circleTxt {
        position: absolute;
        left: 50%;
        top: 50%;
        width: 80%;
        height: 80%;
        padding-top: 50px;
        transform: translate(-50%, -50%);
        font-size: 24px;
        border-radius: 50%;
        color: #fff;
        background-image: linear-gradient(#ff6f92 100%, #e40137 100%);
        .data {
          line-height: 50px;
          font-size: 34px;
        }
      }
    }
  }
  .num {
    height: 240px;
    background-color: #fff;
    text-align: center;
    color: #545671;
    font-size: 44px;

    .title {
      display: block;
      width: 100%;
      padding: 42px 0;
      font-family: PingFangSC-Semibold;
      letter-spacing: 0.41px;
      font-weight: 600;
    }
    .agg {
      width: 100%;
      font-family: PingFangSC-Semibold;
      letter-spacing: 0.69px;
      font-weight: 600;
    }
  }
  .btn {
    width: 100%;
    padding: 30px;
    .van-button {
      width: 100%;
      background-color: #fff;
      height: 100px;
      background: #00b8d4;
      border-radius: 14px;
      color: #fff;
    }
  }
}
.wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
}

.block {
  width: 120px;
  height: 700px;
}

::v-deep .van-overlay {
  background-color: rgba(0, 0, 0, 0);
}
</style>
